package com.kitty.game.item.facade;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.item.config.ChangeCard;
import com.kitty.game.config.ShapePen;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.confirm.model.ChangeCardConfirm;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.item.model.ToOtherChangeCard;
import com.kitty.game.item.service.ChangeCardService;
import com.kitty.game.item.service.PetStoneService;
import com.kitty.game.item.service.handler.MergeItemHelper;
import com.kitty.game.item.service.handler.MergerItemHandler;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.Const;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Controller;

import java.text.MessageFormat;
//后加
import com.kitty.game.item.message.ReqAnswerChangeCard;
import com.kitty.game.item.message.ReqApplyCard;
import com.kitty.game.item.message.ReqCardAddSize;
import com.kitty.game.item.message.ReqMakePill;
import com.kitty.game.item.message.ReqMergeDurableItem;
import com.kitty.game.item.message.ReqSharpPen;
import com.kitty.game.item.message.RespRequestChangeLook;

@Controller
@Slf4j
public class ItemController {

    /**
     * 合并消耗道具
     */
    @RequestMapping
    public void mergeItem(IoSession session, ReqMergeDurableItem mergeDurableItem) {
        Role role = SessionUtils.getRoleBySession(session);
        MergerItemHandler handler = MergeItemHelper.INSTANCE.getHandler(mergeDurableItem.getIndex());
        if (handler != null) {
            handler.mergeItem(role, mergeDurableItem.getItemIdList());
        } else {
            MessagePusher.pushMessage(session, new RespMsg("很抱歉，该功能暂未开放。"));
        }
    }


    /**
     * 使用变身卡
     */
    @RequestMapping
    public void applyCard(IoSession session, ReqApplyCard reqApplyCard) {
        Role role = SessionUtils.getRoleBySession(session);
        Role targetRole = SpringUtils.getRoleService().getOnlinePlayer(reqApplyCard.getRoleId());
        RoleEquip roleEquip = SpringUtils.getEquipService().getRoleEquipByPos(role, reqApplyCard.getPos());
        if (roleEquip == null) {
            return;
        }
        ChangeCard changeCard = SpringUtils.getBean(ChangeCardService.class).getCardByName(roleEquip.getName());
        if (changeCard == null) {
            return;
        }
        String tips = role.getChangeCard() != null ? "你确认要使用#R" + roleEquip.getName() + "#n吗？#r（使用后将#R覆盖#n原变身卡效果）" : "你确认要使用#R" + roleEquip.getName() + "#n吗？";
        if (role.equals(targetRole)) {
            role.setConfirm(new ChangeCardConfirm(targetRole, roleEquip, changeCard));
            RespConfirm respConfirm = new RespConfirm();
            respConfirm.setConfirm_type("bianshen");
            respConfirm.setTips(tips);
            MessagePusher.pushMessage(session, respConfirm);
        } else {
            targetRole.pushTempCache(Const.CHANGE_CARD, new ToOtherChangeCard(role, roleEquip, changeCard));
            MessagePusher.pushMessage(targetRole, new RespRequestChangeLook("#Y" + role.getName() + "#n对你使用了#R" + roleEquip.getName() + "#n，你确定要接受吗？"));
        }

    }


    /**
     * 添加变身卡卡套大小
     */
    @RequestMapping
    public void addCardSize(IoSession session, ReqCardAddSize cardAddSize) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role.getCardSize() >= 500 || cardAddSize.getAddSize() <= 0 || role.getCardSize() + cardAddSize.getAddSize() > 500) {
            return;
        }
        SpringUtils.getBean(ChangeCardService.class).addCardSize(role, cardAddSize.getAddSize());
    }

    /**
     * 妖石合成
     */
    @RequestMapping
    public void makePill(IoSession session, ReqMakePill reqMakePill) {
        Role role = SessionUtils.getRoleBySession(session);
        SpringUtils.getBean(PetStoneService.class).upgradeStone(role, reqMakePill.getIndex(), reqMakePill.getType());
    }


    /**
     * 使用九曲玲珑笔
     */
    @RequestMapping
    public void useSharpPen(IoSession session, ReqSharpPen reqSharpPen) {
        Role role = SpringUtils.getRoleService().getOnlinePlayer(reqSharpPen.getId());
        if (role == null) {
            return;
        }
        SpringUtils.getBean(ChangeCardService.class).useShapePen(session, role, reqSharpPen.getType(), reqSharpPen.getPos());
    }


    /**
     * 同意或者拒绝别人对你使用的 变身卡
     */
    @RequestMapping
    public void answerChangeCard(IoSession session, ReqAnswerChangeCard reqAnswerChangeCard) {
        Role role = SessionUtils.getRoleBySession(session);
        ToOtherChangeCard otherChangeCard = role.popTempCache(Const.CHANGE_CARD);
        if (otherChangeCard == null) {
            return;
        }
        boolean accept = reqAnswerChangeCard.isAccept();
        Role useRole = otherChangeCard.getRole();
        ChangeCard changeCard = otherChangeCard.getChangeCard();
        ShapePen shapePen =otherChangeCard.getShapePen();
        RoleEquip roleEquip = otherChangeCard.getRoleEquip();
        if (!accept) {
            MessagePusher.pushMessage(useRole, new RespMsg(MessageFormat.format("#Y{0}#n拒绝了你的使用#R{1}#n请求。", role.getName(), roleEquip.getName())));
            return;
        }

        /**下面就是接受变身了*/
        if(shapePen!=null){
            SpringUtils.getBean(ChangeCardService.class).useShapePen(useRole, role, roleEquip, shapePen);
            MessagePusher.pushMessage(role, new RespNotifyMiscEx(MessageFormat.format("#Y{0}#n使用#R{1}#n为你增加了#R{2}#n效果。", useRole.getName(), roleEquip.getName(),shapePen.getName())));
            MessagePusher.pushMessage(useRole, new RespNotifyMiscEx(MessageFormat.format("你使用九曲玲珑笔为#Y{0}#n增加了#R{1}#n效果。", role.getName(), shapePen.getName())));
        }else {
            SpringUtils.getBean(ChangeCardService.class).useChangeCard(useRole, role, roleEquip, changeCard);
            MessagePusher.pushMessage(role, new RespNotifyMiscEx(MessageFormat.format("#Y{0}#n耗费了一张#R{1}#n，为你增加了变身卡效果。", useRole.getName(), changeCard.getName())));
            MessagePusher.pushMessage(useRole, new RespNotifyMiscEx(MessageFormat.format("你为#Y{0}#n增加了变身卡效果，耗费了一张#R{1}#n。", role.getName(), changeCard.getName())));
        }
    }
}
